<template>
  <div class="usercenter-container">
    <h2>个人中心</h2>
    <h4>ADMIN USER CENTER</h4>
    <el-divider></el-divider>

    <div class="user-form-container">
      <el-form
        ref="form"
        :model="userForm"
        :rules="userFormRules"
        label-position="left"
        label-width="100px"
      >
        <el-form-item prop="name">
          <el-input v-model="userForm.name" :placeholder="`${name}`">
            <template slot="prepend"
              ><span class="label-box">用户名</span></template
            >
          </el-input>
        </el-form-item>

        <el-form-item prop="loginId">
          <el-input v-model="userForm.loginId" :placeholder="`${loginId}`">
            <template slot="prepend"
              ><span class="label-box">账户ID</span></template
            >
          </el-input>
        </el-form-item>

        <el-form-item prop="loginPwd">
          <el-input
            show-password
            type="password"
            v-model="userForm.loginPwd"
            min="6"
            placeholder="请输入账户密码"
          >
            <template slot="prepend"
              ><span class="label-box">账户密码</span></template
            >
          </el-input>
        </el-form-item>
        <el-form-item prop="confrimPwd">
          <el-input
            show-password
            type="password"
            min="6"
            v-model="userForm.confrimPwd"
            placeholder="请再次输入账户密码"
          >
            <template slot="prepend"
              ><span class="label-box">再次确认</span></template
            >
          </el-input>
        </el-form-item>

        <el-form-item prop="oldLoginPwd">
          <el-input
            show-password
            type="password"
            min="6"
            v-model="userForm.oldLoginPwd"
            placeholder="请输入原登录密码"
          >
            <template slot="prepend"
              ><span class="label-box">原登录密码</span></template
            >
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="info" @click="handlerSubmit"
            >确认用户信息修改</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { updateAdminUser } from "@/api/user";
export default {
  name: "UserCenter",
  data() {
    const handlerCheckConfirmPwd = (r, v, c) => {
      const loginPwd = this.userForm.loginPwd;
      if (loginPwd !== v) {
        c(new Error("两次密码不一致，校验不能通过！"));
      } else {
        c();
      }
    };
    return {
      userForm: {
        name: "",
        loginId: "",
        loginPwd: "",
        confrimPwd: "",
        oldLoginPwd: "",
      },
      userFormRules: {
        loginPwd: [
          { required: true, message: "新密码必填", trigger: "blur" },
          { min: 6, message: "长度必须超过 6 个字符", trigger: "change" },
        ],
        confrimPwd: [
          { required: true, message: "新密码必填", trigger: "blur" },
          { min: 6, message: "长度必须超过 6 个字符", trigger: "change" },
          { validator: handlerCheckConfirmPwd, trigger: "blur" },
        ],
        oldLoginPwd: [
          { required: true, message: "旧密码必填", trigger: "blur" },
          { min: 6, message: "长度必须超过 6 个字符", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["name", "loginId"]),
  },
  methods: {
    handlerSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          const res = await updateAdminUser(this.userForm);
          console.log(res);
          if (typeof res !== "string") {
            this.$store.dispatch("user/updateUserInfo", res.data);
            this.$message.success("用户信息已更新成功！");
            this.$router.push({ path: "/" });
            this.$refs.form.resetFields();
          } else {
            const { msg } = JSON.parse(res);
            this.$message.error(msg);
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.usercenter-container {
  padding: 20px;
  h4 {
    color: gray;
    font-size: 20px;
    font-weight: normal;
    letter-spacing: 30px;
    text-align: center;
  }
  h2 {
    text-align: center;
    letter-spacing: 40px;
  }
  .user-form-container {
    width: 600px;
    position: relative;
    left: 50%;
    transform: translateX(-54%);
  }

  .label-box {
    display: inline-block;
    width: 70px;
    text-align: center;
  }
}
</style>
